package com.uphone.kingmall.adapter;

import android.app.Activity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.china.zhuoyue.R;
import com.uphone.kingmall.activity.GoodsDetailActivity;
import com.uphone.kingmall.bean.HistoryGoodsBean;
import com.uphone.kingmall.utils.ConstansUtils;
import com.uphone.kingmall.utils.GlideUtils;
import com.uphone.kingmall.utils.intent.IntentUtils;

import java.util.List;

/**
 * <pre>
 *     author : radish
 *     e-mail : 15703379121@163.com
 *     time   : 2019/3/22
 *     desc   :
 * </pre>
 */
public class HistoryGoodsItemAdapter extends BaseQuickAdapter<HistoryGoodsBean.DataBean.DateFootsBean, BaseViewHolder> {
    private final Activity activity;
    private final boolean manage;
    private OnGoodsItemClickListener listener;

    public HistoryGoodsItemAdapter(Activity activity, List<HistoryGoodsBean.DataBean.DateFootsBean> list, boolean manage) {
        super(R.layout.kingmall_item_item_history_goods, list);
        this.activity = activity;
        this.manage = manage;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final HistoryGoodsBean.DataBean.DateFootsBean item) {
        ImageView imageView = helper.getView(R.id.iv_image);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imageView.getLayoutParams();
        int width = (ScreenUtils.getScreenWidth() - SizeUtils.dp2px(32)) / 3;
        params.height = width;
        imageView.setLayoutParams(params);
        GlideUtils.getInstance().loadCornerImage(mContext, item.getGoodsMainImg(), SizeUtils.dp2px(2), imageView);
        helper.setText(R.id.tv_price, "¥" + item.getGoodsPrice());
        helper.getView(R.id.ll).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentUtils.getInstance()
                        .with(activity, GoodsDetailActivity.class)
                        .putInt(ConstansUtils.ID, item.getGoodsId())
                        .start();
                if (listener != null) {
                    listener.itemClick(v, helper.getLayoutPosition(), item);
                }
            }
        });
        final CheckBox iv_select = helper.getView(R.id.iv_select);
        iv_select.setChecked(item.isSelect());
        if (manage) {
            iv_select.setVisibility(View.VISIBLE);
            helper.getView(R.id.tv1).setVisibility(View.GONE);
        } else {
            iv_select.setVisibility(View.GONE);
            helper.getView(R.id.tv1).setVisibility(View.VISIBLE);
        }
        iv_select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                item.setSelect(isChecked);
                if (listener != null) {
                    listener.itemSelect(iv_select, helper.getLayoutPosition(), item);
                }
            }
        });
    }


    public void setOnGoodsItemClickListener(OnGoodsItemClickListener listener) {
        this.listener = listener;
    }

    interface OnGoodsItemClickListener {
        void itemClick(View v, int position, HistoryGoodsBean.DataBean.DateFootsBean item);

        void itemSelect(View v, int position, HistoryGoodsBean.DataBean.DateFootsBean item);
    }
}
